﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jinshiyu.Models;

namespace Jinshiyu.Controllers
{
    public class AddressController : Controller
    {
        private JinshiyuEntities db = new JinshiyuEntities();
        //
        // GET: /Address/

        public ActionResult Index()
        {
            C会员 cMember = db.CMembers.Single(c => c.账户 == User.Identity.Name);

            DbSet<FrequentlyUsedAddresses> addresses = db.FrequentlyUsedAddresses;
            IQueryable<FrequentlyUsedAddresses> addr = from a in addresses
                                                   where a.会员编号 == cMember.C会员编号
                                                   select a;

            return View(addresses);
        }

        //
        // GET: /Address/Details/5

        public ActionResult Details(int id)
        {
            var addr = db.FrequentlyUsedAddresses.Single(a => a.ID == id);
            return View(addr);
        }

        //
        // GET: /Address/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Address/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Address/Edit/5
 
        public ActionResult Edit(int id)
        {
            var addr = db.FrequentlyUsedAddresses.Single(a => a.ID == id);
            return View(addr);
        }

        //
        // POST: /Address/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                FrequentlyUsedAddresses addr = db.FrequentlyUsedAddresses.Single(a => a.ID == id);
                addr.城市 = Request.Form["城市"];
                db.SaveChanges();
        
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Address/Delete/5
 
        public ActionResult Delete(int id)
        {
            var addr = db.FrequentlyUsedAddresses.Single(a => a.ID == id);
            return View(addr);
        }

        //
        // POST: /Address/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #region Interfaces for Ajax

//        public JsonResult GetCarList(int id)
//        {
//            var carList = db.FrequentlyUsedAddresses.Take(5);
//            return Json(carList, JsonRequestBehavior.AllowGet);
//        }

        public JsonResult GetAddrList(int id)
        {
            var addrList = db.FrequentlyUsedAddresses.Where(a => a.会员编号 == id).Take(5);
            return Json(addrList, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
